﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using HRM.HRMServiceProxy;

namespace HRM
{
    public partial class frmServiceChargeInfo : Form
    {
        ServiceChargeInfo _SCInfo;
        DialogResult dlgResult;
        RoleRightInfo listRole;
        ServiceChargeClient _wcfSC;
        SCDClient _wcfSCD;
        SalaryClient _wcfSalary;
        bool isChange = false;
        public frmServiceChargeInfo(int SCID, RoleRightInfo _listRole)
        {
            InitializeComponent();
            this.KeyPreview = true;
            dlgResult = DialogResult.Cancel;

            listRole = _listRole;
            LoadLanguge();
            mtxDate.Text = DateTime.Today.ToString("dd/MM/yyyy");
            _wcfSC = Service.ServiceCharge();
            _wcfSCD = Service.SCD();
            _wcfSalary = Service.Salary();
            _SCInfo = new ServiceChargeInfo();
            UISetting.LoadDepartment(ref cboDepartment, 1);
            _SCInfo.ID = SCID;
            BuildVoucherGrid();
            if (SCID > 0)
            {
                cboDepartment.SelectedIndex = 0;
                _SCInfo = _wcfSC.ServiceCharge_GetInfo(SCID);
                Info2Form();

                LoadDataOld();
            }
        }
        void LoadLanguge()
        {
            UpdateUI.Load_ToolStrip(ref toolStrip1);
            UpdateUI.Load_Panel(ref panel2);
            UpdateUI.Load_GroupBox(ref groupBox1);
        }
        void Info2Form()
        {
            mtxDate.Text = Convert.ToDateTime(_SCInfo.Date).ToString("dd/MM/yyyy");
            txtTotalAbsentDays.Text = _SCInfo.TotalAbsentDays.ToString("#,##0.0");
            txtTotalAmount.Text = Convert.ToDecimal(_SCInfo.TotalAmount).ToString("#,##0");
            txtTotalEmployee.Text = _SCInfo.TotalEmployee.ToString();
            txtTotalWorkDays.Text = _SCInfo.TotalWorkDays.ToString();
            isChange = false;
        }

        void Form2Info()
        {
            _SCInfo.Date = UISetting.GetDate(mtxDate.Text);
            _SCInfo.TotalAbsentDays = UISetting.GetDecimalNo(txtTotalAbsentDays.Text);
            _SCInfo.TotalAmount = UISetting.GetDecimalNo(txtTotalAmount.Text);
            _SCInfo.TotalEmployee = UISetting.GetIntNo(txtTotalEmployee.Text);
            _SCInfo.TotalWorkDays = UISetting.GetDecimalNo(txtTotalWorkDays.Text);
            _SCInfo.Description = "";
        }

        bool CheckValid()
        {
            if (!CheckTextValid.IsDateTime(mtxDate.Text, "dd/MM/yyyy"))
            {
                mtxDate.Focus();
                MessageBox.Show(UpdateUI.GetString("ngaychuahople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (!CheckTextValid.IsDecimal(txtTotalAmount.Text))
            {
                txtTotalAmount.Focus();
                MessageBox.Show(UpdateUI.GetString("tongsotienkhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            return true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        void ClearResult()
        {
            dgvUser1.Rows.Clear();
            txtTotalWorkDays.Text = "0";
            txtTotalEmployee.Text = "0";
            txtTotalAbsentDays.Text = "0";
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckValid())
            {
                isChange = true;
                return;
            }
            Form2Info();
            if (_SCInfo.ID > 0)
            {
                if (!listRole.Update)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyencapnhat"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (_wcfSC.ServiceCharge_Exist(_SCInfo.ID, UISetting.GetDate(mtxDate.Text)))
                {
                    MessageBox.Show(UpdateUI.GetString("thangnaydachia"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    ClearResult();
                    return;
                }
                if (_wcfSC.ServiceCharge_Update(_SCInfo))
                {
                    _wcfSC.ServiceCharge_DeleteDetail(_SCInfo.ID);
                    _wcfSC.ServiceCharge_Calculate(_SCInfo.ID, _SCInfo.Date, _SCInfo.TotalAmount);
                    _SCInfo = _wcfSC.ServiceCharge_GetInfo(_SCInfo.ID);
                    Info2Form();
                    LoadDataOld();

                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("capnhatthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    isChange = false;
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isChange = true;
                }
            }
            else
            {
                if (!listRole.Create)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (_wcfSC.ServiceCharge_Exist(_SCInfo.ID, UISetting.GetDate(mtxDate.Text)))
                {
                    MessageBox.Show(UpdateUI.GetString("thangnaydachia"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    ClearResult();
                    return;
                }
                _SCInfo.ID = _wcfSC.ServiceCharge_Insert(_SCInfo);
                if (_SCInfo.ID > 0)
                {
                    _wcfSC.ServiceCharge_Calculate(_SCInfo.ID, _SCInfo.Date, _SCInfo.TotalAmount);
                    _SCInfo = _wcfSC.ServiceCharge_GetInfo(_SCInfo.ID);
                    Info2Form();
                    LoadDataOld();

                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("themmoithanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    isChange = false;
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("themmoikhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isChange = true;
                }
            }
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            if (!listRole.Create)
            {
                MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            _SCInfo.ID = 0;
            mtxDate.Text = DateTime.Today.ToString("dd/MM/yyyy");
            txtTotalAbsentDays.Text = "0";
            txtTotalAmount.Text = "0";
            txtTotalEmployee.Text = "0";
            txtTotalWorkDays.Text = "0";
            dgvUser1.Rows.Clear();

        }
        private void BuildVoucherGrid()
        {
            UISetting.AddColumn(ref dgvUser1, "Employee_FullName", "", "", 140, true, true);
            UISetting.AddColumn(ref dgvUser1, "SCD_AbsentDays", "", "R", 95, true, true);
            UISetting.AddColumn(ref dgvUser1, "SCD_WorkDays", "", "R", 95, true, true);
            UISetting.AddColumn(ref dgvUser1, "SCD_Amount", "", "R", 115, true, true);
            UISetting.AddColumn(ref dgvUser1, "SCD_EmployeeID", "", "R", 130, false, true);
            UISetting.AddColumn(ref dgvUser1, "SCD_ServiceChargeID", "", "R", 120, false, true);
            UISetting.AddColumn(ref dgvUser1, "SCD_TradeUnion", "", "R", 110, true, true);
            UISetting.AddColumn(ref dgvUser1, "SCD_NetPay", "", "R", 120, true, true);
        }

        private void LoadDataOld()
        {
            if (_SCInfo.ID > 0)
            {
                DataSet _ds = _wcfSCD.SCD_GetByServiceCharge(_SCInfo.ID, Convert.ToInt32(cboDepartment.SelectedValue));
                dgvUser1.Rows.Clear();

                for (int i = 0; i < _ds.Tables[0].Rows.Count; i++)
                {
                    dgvUser1.Rows.Insert(i, 1);
                    dgvUser1.Rows[i].Cells["Employee_FullName"].Value = _ds.Tables[0].Rows[i]["Employee_FullName"].ToString();
                    dgvUser1.Rows[i].Cells["SCD_AbsentDays"].Value = Convert.ToDecimal(_ds.Tables[0].Rows[i]["SCD_AbsentDays"]).ToString("#,##0.0");
                    dgvUser1.Rows[i].Cells["SCD_WorkDays"].Value = Convert.ToDecimal(_ds.Tables[0].Rows[i]["SCD_WorkDays"]).ToString("#,##0.0");
                    dgvUser1.Rows[i].Cells["SCD_Amount"].Value = Convert.ToDecimal(_ds.Tables[0].Rows[i]["SCD_Amount"]).ToString("#,##0");
                    dgvUser1.Rows[i].Cells["SCD_ServiceChargeID"].Value = _ds.Tables[0].Rows[i]["SCD_ServiceChargeID"].ToString();
                    dgvUser1.Rows[i].Cells["SCD_EmployeeID"].Value = _ds.Tables[0].Rows[i]["SCD_EmployeeID"].ToString();
                    dgvUser1.Rows[i].Cells["SCD_TradeUnion"].Value = Convert.ToDecimal(_ds.Tables[0].Rows[i]["SCD_TradeUnion"]).ToString("#,##0");
                    dgvUser1.Rows[i].Cells["SCD_NetPay"].Value = Convert.ToDecimal(_ds.Tables[0].Rows[i]["SCD_NetPay"]).ToString("#,##0");
                }
            }
        }
        private void frmServiceChargeInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isChange)
            {
                if (MessageBox.Show(UpdateUI.GetString("bandathaydoidulieu") + "\n" + UpdateUI.GetString("coluutruockhithoatkhong"), UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    btnSave_Click(null, null);
                    if (isChange)
                        e.Cancel = true;
                    else
                        e.Cancel = false;
                }
            }
            _wcfSalary.Close();
            _wcfSC.Close();
            _wcfSCD.Close();
            this.DialogResult = dlgResult;
        }

        private void frmServiceChargeInfo_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    this.Dispose();
                    this.Close();
                    break;
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.F3:
                    btnSave_Click(null, null);
                    break;
                case Keys.Enter:
                    if (!e.Alt && !e.Control)
                    {
                        this.ProcessTabKey(!e.Shift);
                        e.Handled = true;
                    }
                    break;
            }
        }

        private void frmServiceChargeInfo_Activated(object sender, EventArgs e)
        {
            if (dgvUser1.Columns.Count > 0)
            {
                dgvUser1.Columns["Employee_FullName"].HeaderText = UpdateUI.GetString("hovaten");
                dgvUser1.Columns["SCD_AbsentDays"].HeaderText = UpdateUI.GetString("songaynghi1");
                dgvUser1.Columns["SCD_WorkDays"].HeaderText = UpdateUI.GetString("songaycong");
                dgvUser1.Columns["SCD_Amount"].HeaderText = UpdateUI.GetString("tongsotien");
                dgvUser1.Columns["SCD_TradeUnion"].HeaderText = UpdateUI.GetString("phicongdoan");
                dgvUser1.Columns["SCD_NetPay"].HeaderText = UpdateUI.GetString("tienthuclinh");
            }
        }


        private void btnPrint_Click(object sender, EventArgs e)
        {
            if (_SCInfo.ID > 0)
            {
                frmPreviewReport f = new frmPreviewReport("ServiceCharge", new string[] { _SCInfo.ID.ToString() });
                f.ShowDialog();
            }
        }

        private void cboDepartment_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_SCInfo.ID > 0)
            {
                LoadDataOld();
            }
        }

        private void DataChange(object sender, EventArgs e)
        {
            isChange = true;
        }
    }
}
